package com.hrz.datasync.service.impl;

import com.hrz.datasync.common.RetResponse;
import com.hrz.datasync.common.RetResult;
import com.hrz.datasync.common.TimeExchange;
import com.hrz.datasync.kafka.producer.TpAdZoneProducer;
import com.hrz.datasync.pojo.primary.TpAdZone;
import com.hrz.datasync.pojo.secondary.BbqAdZone;
import com.hrz.datasync.repository.primary.TpAdZoneRepository;
import com.hrz.datasync.repository.secondary.BbqAdZoneRepository;
import com.hrz.datasync.service.TpAdZoneService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @作 者: 侯建军
 * @日 期: 2018/11/05 10:54:54
 * @描 述: 实现类
 */
@Service
@Log
public class TpAdZoneServiceImpl implements TpAdZoneService {
    @Autowired
    private TpAdZoneRepository tpAdZoneRepository;
    @Autowired
    private BbqAdZoneRepository bbqAdZoneRepository;
    @Autowired
    private TpAdZoneProducer tpAdZoneProducer;

    /**
     * 功能描述:
     * 用来写BbqAdZone表
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/05 04:00:22
     */
    @Override
    @Transactional
    public RetResult addTpAdZone(BbqAdZone bbqAdZone) {
        TpAdZone tpAdZone=this.bbqAdZoneToTpAdZone(bbqAdZone);
        tpAdZone.setSync(1);
        TpAdZone tpAdZone0 = tpAdZoneRepository.save(tpAdZone);
        bbqAdZone.setSync(1);
        //修改原记录
        bbqAdZoneRepository.saveAndFlush(bbqAdZone);
        if (null != tpAdZone0) {
            log.info("成功：" + tpAdZone0);
            return RetResponse.makeOKRsp(tpAdZone0);
        } else {
            log.info("失败：" + tpAdZone);
            return RetResponse.makeErrRsp("失败" + tpAdZone0);
        }
    }

    /**
     * 功能描述:
     * 用来发送消息
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/05 03:58:21
     */
    @Override
    public RetResult sendTpAdZone(TpAdZone tpAdZone) {
        tpAdZoneProducer.adZoneSend(tpAdZone);
        return RetResponse.makeOKRsp();
    }

    /**
     * 功能描述:
     * 数据转换类，将BbqAdZone转为BbqAdZone
     *
     * @参 数:
     * @返 回:
     * @作 者: 侯建军
     * @日 期: 2018/11/05 04:07:35
     */
    public TpAdZone bbqAdZoneToTpAdZone(BbqAdZone bbqAdZone) {
        TpAdZone tpAdZone = new TpAdZone();
        tpAdZone.setAddtime(TimeExchange.DateToTimestamp(bbqAdZone.getTimeInsert()));
        tpAdZone.setIsdelete(bbqAdZone.getDeleted());
        tpAdZone.setAlmmid(bbqAdZone.getPid1().toString());
        tpAdZone.setSiteid(bbqAdZone.getPid2().toString());
        tpAdZone.setAdzoneId(bbqAdZone.getPid3().toString());
        tpAdZone.setAdzoneName(bbqAdZone.getAdzoneName());
        tpAdZone.setSelf(bbqAdZone.getSelf());
        return tpAdZone;
    }

}
